package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/14 14:07
 */
@RestController
@CrossOrigin
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getOrder(@PathVariable("orderId") String orderId) throws InterruptedException {
        log.info("获取商品列表：{}", orderId);
        // 模拟90%的阻塞
        if (Math.random() < 0.9) {
            int delayTime = new Random().nextInt(5000);
            log.info("阻塞：{}",delayTime);
            Thread.sleep(delayTime);

        }
//        Thread.sleep(2000);
        return JsonResult.build(JsonResult.SUCCESS, itemService.getItems(orderId));
    }

    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> list) {
        itemService.decreaseNumbers(list);
        return JsonResult.build().code(JsonResult.SUCCESS).msg("减少库存成功");
    }


    @GetMapping("/favicon.ico")
    public void ico() {

    }
}
